var boxMsg=$("#box-message");			
var boxMusic=$("#box-music");
var boxTips=$("#tip-message");

function showMsg(msg) {
    $(boxMsg).find("p").html(msg);
    $(boxMsg).modal('show');
}

function removeTag(id) {
    $("#" + id).parent().fadeOut("fast",
    function() {
        $("#" + id).parent().remove();
    });
    fillTags();
}

function fillTags() {
    var tags = "";
    $.each($("#song_tag_list").find(".s_tag_name"),
    function(i, n) {
        tags += $.trim($(n).html()) + ",";
    });
    $("#form_tags").val(tags);
}

$(document).ready(function() {

	document.forms[0].reset();
	document.forms[1].reset();

	$(boxMsg).modal({backdrop:"static"});
	$(boxMsg).find("a[name='modal-ok-btn']").click(function(){
        $(boxMsg).modal('hide');
    });
    
    $(boxMusic).modal({backdrop:"static"});
	$(boxMusic).find("a[name='modal-exit-btn']").click(function(){
        $(boxMusic).modal('hide');
    });
    
    $("#open-upload-box-btn").click(function(){
    	 $(boxMusic).modal('show');
    });
    
    $("#re-upload-box-btn").click(function(){
    	 $(boxMusic).modal('show');
    });
    
    var uploadSongBtn=$(boxMusic).find("input[name='upload-song-btn']");
	$(uploadSongBtn).button('reset');
	$("#form-song-upload").ajaxForm({dataType:"html", success:function (data) {
		var result=$.parseJSON(data);
		if(result.code!="1000"){
			$(uploadSongBtn).val("上传");
			$(uploadSongBtn).button('complete');
			$("#msg-bar").show();
			$("#msg-bar").addClass("error");
			$(boxMusic).find("p").html(result.msg);
		}else{
			$(uploadSongBtn).val("上传");
			$(uploadSongBtn).button('complete');
			$(boxMusic).modal('hide');
			$("#open-upload-box-btn").hide();
			$("#audio-upload-info").show();
			$("#audio-upload-name").html(result.fileName+".mp3");
			$("input[name=form_name]").val(result.fileName);
			$("input[name=form_file]").val(result.objectName);
		}
	}});
	
	$(uploadSongBtn).click(function (e) {
		$("#msg-bar").hide();
		$("#form-song-upload").submit();
		$(uploadSongBtn).val("上传中...");
		$(uploadSongBtn).button('loading');
		return false;
	});

    $('.tabs').tabs();

    $(".tag").click(function() {
        var name = $(this).html();
        var id = $(this).attr("id");
        var count = $("#song_tag_list").find("span").length;
        if (count >= 10) {
            showMsg("标签个数超过10个了哦,请精心挑选几个吧。");
            return;
        }
        if ($("#stag_" + id).html() == null) {
            var stag = "<span class='stag'><a id='stag_" + id + "' class='s_tag_name'>" + name + "</a><a title='删除标签' onclick=\"removeTag('stag_" + id + "');return false;\">X</a></span>";
            $("#song_tag_list").append(stag);
        }

        fillTags();

    });

    var optionMsg = {
        form_name: '推荐歌曲的名称',
        form_desc: '推荐感言,长度不超过140个字符'
    },
    validateError = {
        form_name: {
            isNull: '歌曲的名字不能为空',
            invalidLength: '长度不超过30个字符'
        },
        form_desc: {
            isNull: '推荐感言不能为空',
            invalidLength: '长度不超过140个字符'
        }
    },
    validateRules = {
        form_name: {
            elems: 'input[name=form_name]',
            isNull: function(el) {
                return ! $.trim(el.val());
            },
            invalidLength: function(el) {
                var desc = $.trim(el.val());
                return desc.length > 30;
            }
        },
        form_desc: {
            elems: 'textarea[name=form_desc]',
            isNull: function(el) {
                return ! $.trim(el.val());
            },
            invalidLength: function(el) {
                var desc = $.trim(el.val());
                return desc.length > 140;
            }
        }
    };
    $('#song_form').validateForm(validateRules, validateError, optionMsg, null);
    
    $(boxTips).modal({backdrop:true});
	$(boxTips).find("a[name='modal-ok-btn']").click(function(){
        $(boxTips).modal('hide');
        $("#open-upload-box-btn").trigger("click");
    });
    
    $("#post-button").click(function(){
    	if($("input[name=form_file]").val()==""){
    		$(boxTips).modal('show');
    	}else{
    		$('#song_form').submit();
    	}
    });
    
    
});